package com.xingxuecn.limit.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.xingxuecn.limit.common.Constant;
import com.xingxuecn.limit.common.model.vo.ResultModel;
import com.xingxuecn.limit.common.model.vo.TreeNode;
import com.xingxuecn.limit.common.utils.ResponseUtils;
import com.xingxuecn.limit.service.MenuService;
import com.xingxuecn.limit.service.UserService;
import com.xingxuecn.limit.service.impl.MenuServiceImpl;
import com.xingxuecn.limit.service.impl.UserServiceImpl;

import org.apache.struts2.ServletActionContext;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Kris at 2018/9/5 14:37
 */
public class IndexAction extends ActionSupport {
    private MenuService menuService = new MenuServiceImpl();
    private UserService userService = new UserServiceImpl();

    private String account;
    private String pwd;

    public String index(){
        return SUCCESS;
    }

    /**
     * 登陆Action
     * @return
     */
    public String doLogin(){
        ResultModel resultModel = userService.doLogin(account,pwd);
        ResponseUtils.writeJson(resultModel,ServletActionContext.getResponse());
        return null;
    }

    public String quitLogin(){
        ServletActionContext.getRequest().getSession().setAttribute(Constant.USER_SESSION_KEY,null);
        ResponseUtils.writeJson(new ResultModel(200,"您已退出登陆"),ServletActionContext.getResponse());
        return null;
    }
    
    public String loadMenuTree() throws SQLException {
        HttpServletResponse response = ServletActionContext.getResponse();
        List<TreeNode> treeNodeList = menuService.menuTree();
        ResponseUtils.writeJson(treeNodeList,response);
        return null;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
